#!/usr/bin/python
import os, sys, re

if len(sys.argv)==1:
    print "Usage: LogAnalyze $LogFile$"
    sys.exit(1)
suposedFile = sys.argv[1]
if suposedFile == "--help":
    print "Usage: LogAnalyze \$LogFile\$"
if os.path.isfile(suposedFile)==False:
    print "please input an log file"
    sys.exit(1)

class MethodLog:
    methodName = ""
    times = []
    def __init__(self, methodName, times):
        self.methodName = methodName
        self.times = times
    def analyze(self):
        min = self.times[0]
        max = self.times[0]
        for time in self.times:
            if time > max:
                max = time
            if time < min:
                min = time
        print ">>%s: total %d times, min time is %d ms, max time is %d ms." % (self.methodName, len(self.times), min, max)
        print "times:", sorted(self.times)
 
mtdDict = {}
for line in open(suposedFile).readlines():
    #[INFO]xxx.getFactionRankingListByCriteria(xxx.RankingSearchCriteriaTO): 4482ms
    pattern = re.compile("^[\[].*\.(.*)\(.*\).*\s+(\d+)(ms)$")
    match = pattern.match(line)
    if match:
        method = match.group(1)
        time = eval(match.group(2))
        if mtdDict.get(method)==None:
            mtdDict[method] = [time]
        else:
            mtdDict[method].append(time)
print suposedFile
for key in mtdDict.keys():
    methodLog = MethodLog(key, mtdDict[key])
    methodLog.analyze()

